Photo by Fabio Consilvio on Unsplash
上一篇埋個伏筆,今天我們來玩 Swift package 吧。
今天介紹用FLEX加入到 Swift package。
Copy HTTPS 的 URL, 評估一下選多少 Releases version
Xcode 導覽頁 -> File -> Add Packages
設定一下 Up to Next Major Version
Add Packages 後
FLEX 進入專案囉~
Add new Group, 做好專案資料整理。
建個 Managers 資料夾
command + N , create a new Swift File
import SwiftUI
#if DEBUG
import FLEX
#endif
class LibsManager {
static let shared = LibsManager()
func setupLibs() {
// No need for UIWindow in SwiftUI
let libsManager = LibsManager.shared
libsManager.setupFLEX()
}
func setupFLEX() {
#if DEBUG
FLEXManager.shared.isNetworkDebuggingEnabled = true
#endif
}
}
extension LibsManager {
func showFlex() {
#if DEBUG
FLEXManager.shared.showExplorer()
#endif
}
}
我們用Singleton pattern 來管理套件。
初始化 LibsManager.shared.setupLibs()
init() {
LibsManager.shared.setupLibs()
}
放這裡
@main
struct SwimmingStrokesApp: App {
init() {
LibsManager.shared.setupLibs()
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
加個神奇的按鈕
加個神奇的按鈕
import SwiftUI
struct FlexButton: View {
#if DEBUG
let screenSize = UIScreen.main.bounds.size
var body: some View {
Button(action: {
LibsManager.shared.showFlex()
}) {
Text("F")
.frame(width: 50, height: 50)
.background(Color.orange)
.foregroundColor(.white)
.clipShape(Circle())
.padding(.all, 10)
}
// TODO: 先不要
// .position(x: screenSize.width - 35, y: screenSize.height - 75)
}
#else
var body: some View {
EmptyView()
}
#endif
}
struct FlexButton_Previews: PreviewProvider {
static var previews: some View {
FlexButton()
}
}
加這段 code
#if DEBUG
FlexButton()
#endif
FLEX 很多功能,可以去玩玩看,
我要放連假囉,下集待續